package com.atguigu.stuty.pool;

import java.util.concurrent.*;

/**
 * @Author early morning
 * @Create 2025/4/25 21:50
 * @Description:
 */
public class ThreadPoolTest {
    private static void defaultPool() {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);// 一池固定数
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();// 一池1线程
        //ExecutorService threadPool = Executors.newCachedThreadPool();// 一池N线程

        try {
            for (int i = 0; i < 10; i++) { //20个顾客（请求线程）来办理业务
                int finalI = i;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName()+"\t"+"为："+ finalI +"\t客户办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() + 1,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        try {
            for (int i = 1; i <= 10; i++) { //5个顾客（请求线程）来办理业务
                int finalI = i;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName()+"\t"+"为"+ finalI +"号客户办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
